package com.geek.jdbc.trans.customers;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
import com.geek.jdbc.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class InsertCustomer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入编号：");
        int id = scanner.nextInt();
        scanner.nextLine();
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入邮箱：");
        String email = scanner.nextLine();
        System.out.print("请输入生日（要求按xxxx-xx-xx格式）：");
        String birthday = scanner.nextLine();

        String sql = "INSERT INTO customers(id, name, email, birthday) VALUES (?,?,?,?)";
        try (Connection conn = JDBCUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, email);
            pstmt.setDate(4, java.sql.Date.valueOf(birthday));
            int affectedRows = pstmt.executeUpdate();
            if (affectedRows > 0) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}